﻿package com.fox2d 
{
	import flash.display.BitmapData;
	import com.fox2d.events.GameKeyEvent;
	import com.fox2d.Game2D;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Orlando Leite
	 */
	public class Hero2D extends Char2D
	{
		protected var keyList:Dictionary = new Dictionary();
		protected var refKeyList:Dictionary = new Dictionary();
		
		protected var controller:GameController;
		
		public function Hero2D()
		{
			
		}
		
		override public function init( game:Game2D, walkMap:BitmapData ):void 
		{
			super.init( game, walkMap );
			
			controller = GameController.controller();
			
			controller.addEventListener( GameKeyEvent.BTN_DOWN, buttonDownHandle );
			controller.addEventListener( GameKeyEvent.BTN_UP, buttonUpHandle );
		}
		
		public function addButton( gameKey:String, func:Function ):void
		{
			keyList[gameKey] = func;
			refKeyList[func] = gameKey;
		}
		
		public function setDefaultKeyboard( up:String = KeyboardGame.UP, down:String = KeyboardGame.DOWN, left:String = KeyboardGame.LEFT, right:String = KeyboardGame.RIGHT ):void
		{
			addButton( up, moveNorth );
			addButton( down, moveSouth );
			addButton( left, moveWest );
			addButton( right, moveEast );
		}
		
		protected function buttonDownHandle( event:GameKeyEvent ):void
		{
			var temp:Function = keyList[event.btn];
			
			if ( temp != null )
			{
				temp.call();
			}
		}
		
		protected function buttonUpHandle( event:GameKeyEvent ):void
		{
			if ( controller.getKeyState( refKeyList[moveNorth] ) )
			{
				moveNorth();
			}
			else if ( controller.getKeyState( refKeyList[moveSouth] ) )
			{
				moveSouth();
			}
			else if ( controller.getKeyState( refKeyList[moveWest] ) )
			{
				moveWest();
			}
			else if ( controller.getKeyState( refKeyList[moveEast] ) )
			{
				moveEast();
			}
			else
			{
				stopMove();
			}
		}
	}
	
}